<template>
	<view class="main-page">
		<view class="search-box">
			<view class="input-box">
				<view class="row-container width-100p">
					<image class="wh-30 mr-20" src="../../static/common/ic_search.png" 
					style="flex-shrink: 0;"></image>
					<input 
						v-model="keyword"
						class="font-333-28 width-100p"
						placeholder-class="font-999-28"
						placeholder="请输入客户名称或电话"
						@input="handleSearchClient"
					/>
				</view>
				<image class="wh-34 ml-20" src="../../static/common/ic_clear.png" v-show="keyword"
				 @click="clearInput" style="flex-shrink: 0;"></image>
			</view>
		</view>
		
		<view class="column-container width-100p mt-133 bg-white mb-150" >
			<view class="list-item" v-for="(item ,index) in clientList" :key="index"
			@click="handleChosenClient(item)">
				
				<image class="chosen-flag" src="../../static/common/ic_chosen_true.png"
				v-if="chosenList.length > 0 && chosenList.findIndex((chosenItem) =>chosenItem && chosenItem.customId == item.id) != -1"></image>
				 
				<image class="chosen-flag" src="../../static/common/ic_chosen_false.png"
				v-else></image>
			
				<view class="row-container width-100p">
					<image class="avatar" src="../../static/common/ic_avatar_female.png" v-if="item.sex == 0"></image>
					<image class="avatar" src="../../static/common/ic_avatar_male.png" v-if="item.sex == 1"></image>
					<view class="column-container mt-4">
						<text class="font-333-32 font-weight-600">{{item.realName}}</text>
						<view class="row-container mt-4">
							<text class="font-333-26">ID：{{item.id}}</text>
							<view class="column-line"></view>
							<text class="font-333-26" v-if="item.sex == 1">男</text>
							<text class="font-333-26" v-if="item.sex == 0">女</text> 
							<view class="column-line"></view>
							<text class="font-333-26">联系电话：{{item.mobile}}</text>
						</view>
					</view>
				</view>
				<view class="source-box">
					<text class="font-999-22" v-if="item.channelMsg">来源：{{item.channelMsg}}</text>
					<view class="column-line" v-if="item.channelMsg"></view>
					<text class="font-999-22" v-if="item.brithdayTimeStr">生日：{{item.brithdayTimeStr}}</text>
					<view class="column-line" v-if="item.brithdayTimeStr"></view>
					<text class="font-999-22" v-if="item.lastShopTime">上次到店：{{item.lastShopTime}}</text>
					<text class="font-999-22" v-else>暂无到店记录</text>
				</view>
			</view>
		</view>
		
		<view class="bottom-box">
			<view class="chosen-count-box" @click="showConfirmWindow = true">
				<text class="font-333-32 font-weight-600 ml-24">已选客户管理</text>
				<view class="row-container mr-24">
					<text class="font-f94-32 font-weight-600">{{chosenList.length}}</text>
					<text class="font-333-32 font-weight-600">/{{joinCount}}</text>
					<image class="wh-20 ml-4" src="../../static/common/ic_arrow_right.png"></image>
				</view>
			</view>
			<text class="add-client" @click="handleAddClient">添加至预约</text>
		</view>
		
		
		<!-- 客户管理弹窗 -->
		<view class="window-main" style="justify-content: flex-end;" v-if="showConfirmWindow">
			<view class="confirm-window">
				<text class="font-333-36 font-weight-600">已选客户管理</text>
				<text class="count">已选{{chosenList.length}}人</text>
				
				<view class="column-container width-100p mt-40 mb-145">
					<view class="list-item" v-for="(chosenItem , chosenIndex) in chosenList" 
					:key="chosenIndex" >
						<image class="chosen-flag" src="../../static/common/ic_remove.png"
						@click="handleChosenClientWindow(chosenItem)"></image>
									
						<view class="row-container width-100p">
							<image class="avatar" src="../../static/common/ic_avatar_female.png" v-if="chosenItem.sex == 0"></image>
							<image class="avatar" src="../../static/common/ic_avatar_male.png" v-if="chosenItem.sex == 1"></image>
							<view class="column-container mt-4">
								<text class="font-333-32 font-weight-600">{{chosenItem.realName}}</text>
								<view class="row-container mt-4">
									<text class="font-333-26">ID：{{chosenItem.id}}</text>
									<view class="column-line"></view>
									<text class="font-333-26" v-if="chosenItem.sex == 1">男</text>
									<text class="font-333-26" v-if="chosenItem.sex == 0">女</text> 
									<view class="column-line"></view>
									<text class="font-333-26">联系电话：{{chosenItem.mobile}}</text>
								</view>
							</view>
						</view>
						<view class="source-box">
							<text class="font-999-22" v-if="chosenItem.channelMsg">来源：{{chosenItem.channelMsg}}</text>
							<view class="column-line" v-if="chosenItem.channelMsg"></view>
							<text class="font-999-22" v-if="chosenItem.brithdayTimeStr">生日：{{chosenItem.brithdayTimeStr}}</text>
							<view class="column-line" v-if="chosenItem.brithdayTimeStr"></view>
							<text class="font-999-22" v-if="chosenItem.lastShopTime">上次到店：{{chosenItem.lastShopTime}}</text>
							<text class="font-999-22" v-else>暂无到店记录</text>
						</view>
					</view>
				</view>
				
				<view class="confirm-btn-box">
					<text class="confirm-cancel" @click="showConfirmWindow = false">关闭</text>
					<text class="confirm" @click="handleAddClient">确认预约</text>
				</view>
				
			</view>
		</view>
		
		<!-- 确认取消弹窗 -->
		<view class="window-main" v-if="showCancelAppointWindow">
			<view class="cancel-window">
				<text class="font-333-36 font-weight-600 ml-10">确认取消用户预约吗？</text>
				<text class="font-666-32 mt-10 ml-10">请与用户沟通后再取消预约，谨慎操作！</text>
				<view class="row-container width-100p mt-40 ml-10">
					<text class="font-f23-28 font-weight-600">*</text>
					<text class="font-333-28 font-weight-600">请选择取消预约原因</text>
				</view>
				<view class="row-container width-100p mt-20" style="flex-wrap: wrap;">
					<text v-for="(reasonItem , reasonIndex) in cancelReasonList" :key="reasonIndex"
					:class="{'chosen-reason-item' : chosenCancelReason == reasonItem , 
					'reason-item' : chosenCancelReason != reasonItem}" 
					@click="changeCancelReason(reasonItem)">
						{{reasonItem}}
					</text>
				</view>
				
				<view class="row-container-row-between width-100p mt-40">
					<text class="cancel-cancel" @click="handleClickCloseCancelWindow">取消</text>
					<text :class="{'cancel-confirm-negative' : !chosenCancelReason , 
					'cancel-confirm' : chosenCancelReason}" @click="handleCancelAppoint">确定</text>
				</view>
				
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				clientList:[],
				page:1,
				hasMoreData:false,
				keyword:'',
				chosenList:[] , // 已选客户列表
				
				joinCount:0 , //可选总人数
				showConfirmWindow:false,
				
				
				cancelCustomList:[] , // 取消的客户列表
				clickCancelItem:null , // 点击取消的item
				showCancelAppointWindow:false,
				cancelReasonList:[],
				chosenCancelReason:'',
			}	
		},
		
		onLoad(options) {
			this.joinCount = options.joinCount
			
			if (options.chosenList){
				this.chosenList = JSON.parse(options.chosenList)
			}
			
			if (options.cancelCustomList){
				this.cancelCustomList = JSON.parse(options.cancelCustomList)
			}
			
			this.loadCancelReasonList()
			this.loadDataList()
		},	
		
		onPullDownRefresh() {
			this.page = 1
			this.loadDataList()
		},
		
		onReachBottom() {
			if (this.hasMoreData){
				this.page ++
				this.loadDataList()
			}
		},
		
		methods: {
			loadCancelReasonList(){
				let that = this
				this.http({
					url:"/common/cancelList",
					method:"POST",
					hideLoading:true,
					data:{},
					success(data){
						if (data.code == 200){
							that.cancelReasonList = data.data
						}
					},
					failed(e){
						
					}
				})
			},
			
			//选择取消预约的原因
			changeCancelReason(item){
				this.chosenCancelReason = item
			},
			
			handleClickCloseCancelWindow(){
				this.clickCancelItem = null
				this.chosenCancelReason = ""
				this.showCancelAppointWindow = false
			},
			
			//确认取消
			handleCancelAppoint(){
				if (this.clickCancelItem){
					this.$set(this.clickCancelItem , 'cancelMsg' , this.chosenCancelReason)
					let index = this.chosenList.findIndex((chosenItem) => this.clickCancelItem.customId == chosenItem.customId)
					this.chosenList.splice(index , 1)
					
					let cancelIndex = this.cancelCustomList.findIndex((cancelItem) =>
					 cancelItem.id == this.clickCancelItem.id)
					if (cancelIndex == -1){
						this.cancelCustomList.push(JSON.parse(JSON.stringify(this.clickCancelItem)))
					}
					
					this.clickCancelItem = null
					this.chosenCancelReason = ""
					this.showCancelAppointWindow = false
				}
			},
			
			handleSearchClient(e){
				this.keyword = e.detail.value
				this.page = 1
				this.loadDataList()
			},
			
			clearInput(){
				this.keyword = ""
				this.page = 1
				this.loadDataList()
			},
			
			loadDataList(){
				let that = this
				this.http({
					url:'/user/custom/findCustomList',
					method:'POST',
					data:{
						pageNo: that.page,
						pageSize: 20,
						keyWord:this.keyword
					},
					success(res){
						if (res.code == 200){
							if (that.page == 1){
								that.clientList = res.data
							} else{
								that.clientList = [...that.clientList, ...res.data]
							}
							that.hasMoreData = res.data.length >= 20
						}
						uni.stopPullDownRefresh()
					},
					failed(e){
						uni.stopPullDownRefresh()
					}
				})
			},
			
			//列表中点击选中或者取消选中
			handleChosenClient(item){
				let	index = this.chosenList.findIndex((chosenItem) => chosenItem && chosenItem.customId == item.id) 
				if (index == -1){
					if (this.chosenList.length < this.joinCount){
						let	cancelIndex = this.cancelCustomList.findIndex((cancelItem) => cancelItem.customId == item.id)
						if (cancelIndex == -1){
							let data = {"id" : "" , "customId" : item.id ,
							"realName" : item.realName , 'sex' : item.sex , "mobile" : item.mobile}
							this.chosenList.push(data)
						} else{
							this.chosenList.push(this.cancelCustomList[cancelIndex])
							this.cancelCustomList.splice(cancelIndex , 1)
						}
						
					} else{
						uni.showToast({
							title:'超出本次预约人数上限，无法再继续添加客户',
							icon:'none'
						})
					}
				} else{
					let chosenData = null
					
					if (this.chosenList.length > 0){
						this.chosenList.forEach((cItem) =>{
							console.log('citem-----' , cItem)
							if (item.id == cItem.customId){
								chosenData = cItem
							}
						})
					}
					
					console.log('chosenData----' , chosenData)
					if (chosenData && chosenData.id){
						this.clickCancelItem = chosenData
						this.showCancelAppointWindow = true
					} else{
						this.chosenList.splice(index , 1)
					}
					
				}
			},
			
			//客户管理弹窗中  点击选中或取消选中
			handleChosenClientWindow(item){
				let	index = this.chosenList.findIndex((chosenItem) => chosenItem && chosenItem.customId == item.customId) 
				if (index != -1){
					if (item.id){
						this.clickCancelItem = item
						this.showCancelAppointWindow = true
					} else{
						this.chosenList.splice(index , 1)
					}
					
				}
			},
			
			handleAddClient(){
				if (this.chosenList.length < 1){
					uni.showToast({
						title:'请至少选择一位客户',
						icon:'none'
					})
					return
				}
				uni.$emit('chosenMultiClient' , this.chosenList)
				uni.$emit('chosenMultiClientCancel' , this.cancelCustomList)
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.search-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 113rpx;
		background: white;
		position: fixed;
		z-index: 100;
		
		.input-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			background: #F5F5F5;
			border-radius: 10rpx;
			width: calc(100% - 100rpx);
			height: 70rpx;
			margin: 0rpx 30rpx;
			padding: 0rpx 20rpx;
			
		}
	}
	
	.list-item{
		display: flex;
		flex-direction: column;
		width: calc(100% - 60rpx);
		margin: 0rpx 30rpx;
		align-items: center;
		position: relative;
		padding: 25rpx 0rpx;
		
		.chosen-flag{
			width: 32rpx;
			height: 32rpx;
			position: absolute;
			top: 25rpx;
			right: 0;
		}
		
		.avatar{
			width: 80rpx;
			height: 80rpx;
			margin-right: 24rpx;
		}
		
		.column-line{
			width: 1rpx;
			height: 16rpx;
			background: #999999;
			margin: 0rpx 16rpx;
		}
		
		.source-box{
			background: #F7F7F7;
			border-radius: 4rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			width: calc(100% - 28rpx);
			padding: 6rpx 14rpx;
			margin-top: 8rpx;
		}
	}
	
	.bottom-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: calc(100% - 60rpx);
		height: 120rpx;
		background: white;
		padding: 0rpx 30rpx;
		position: fixed;
		bottom: 0;
		
		.chosen-count-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			background: #F5F5F5;
			border-radius: 10rpx;
			margin-right: 30rpx;
			width: 402rpx;
			height: 88rpx;
		}
		
		.add-client{
			flex-grow: 1;
			height: 88rpx;
			background: #F94C4B;
			border-radius: 10rpx;
			line-height: 88rpx;
			text-align: center;
			color: white;
			font-size: 32rpx;
			font-weight: 600;
		}
	}
	
	.confirm-window{
		display: flex;
		flex-direction: column;
		align-items: center;
		width: calc(100% - 20rpx);
		height: 1000rpx;
		padding: 40rpx 10rpx;
		background: white;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		overflow-y: auto;
		position: relative;
		
		.count{
			color: #666666;
			font-size: 28rpx;
			position: absolute;
			top: 46rpx;
			right: 30rpx;
		}
		
		.confirm-btn-box{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			width: calc(100% - 80rpx);
			margin-left: 40rpx;
			margin-right: 40rpx;
			height: 85rpx;
			background: white;
			position: fixed;
			bottom: 0rpx;
			padding-bottom: 40rpx;
			
			.confirm-cancel{
				width: 200rpx;
				height: 85rpx;
				background: #F5F5F5;
				border-radius: 10rpx;
				line-height: 85rpx;
				text-align: center;
				margin-right: 30rpx;
				color: #333333;
				font-size: 32rpx;
				font-weight: 600;
			}
			
			.confirm{
				flex-grow: 1;
				height: 85rpx;
				background: #F94C4B;
				border-radius: 10rpx;
				line-height: 85rpx;
				text-align: center;
				color: white;
				font-size: 32rpx;
				font-weight: 600;
			}
		}
	}
	
	.cancel-window{
		display: flex;
		flex-direction: column;
		align-items: center;
		width: calc(100% - 198rpx);
		margin: 0rpx 69rpx;
		padding: 40rpx 30rpx;
		background: white;
		border-radius: 30rpx;
		
		.reason-item{
			border: #F94C4B solid 1rpx;
			padding: 18rpx 22rpx;
			font-size: 28rpx;
			color: #F94C4B;
			border-radius: 10rpx;
			margin: 0rpx 10rpx 20rpx 10rpx;
			font-weight: 600;
		}
		
		.chosen-reason-item{
			padding: 18rpx 22rpx;
			font-size: 28rpx;
			color: white;
			background: #EC5150;
			border-radius: 10rpx;
			margin: 0rpx 10rpx 20rpx 10rpx;
			font-weight: 600;
		}
		
		.cancel-cancel{
			width: 200rpx;
			height: 85rpx;
			background: #F5F5F5;
			border-radius: 10rpx;
			margin-right: 30rpx;
			text-align: center;
			line-height: 85rpx;
			color: #333333;
			font-size: 32rpx;
			font-weight: 600;
		}
		
		.cancel-confirm{
			flex-grow: 1;
			height: 85rpx;
			border-radius: 10rpx;
			background: #F94C4B;
			text-align: center;
			line-height: 85rpx;
			color: white;
			font-size: 32rpx;
			font-weight: 600;
		}
		
		.cancel-confirm-negative{
			flex-grow: 1;
			height: 85rpx;
			border-radius: 10rpx;
			background: #F94C4B;
			text-align: center;
			line-height: 85rpx;
			color: white;
			font-size: 32rpx;
			font-weight: 600;
			opacity: 0.5;
		}
	}
	
</style>
